using JT808Protocol.Common;
using JT808Protocol.Models.Enums;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyParserStrategy.DeviceParams
{
    /// <summary>
    /// String 型参数
    /// </summary>
    public class DpString : IDeviceParam
    {
        [JsonIgnore]
        public byte Length { get; set; }

        [JsonIgnore]
        public uint ParamID { get; set; }

        [JsonPropertyName("参数类型")]
        public EnumDeviceParamType ParamType
        {
            get
            {
                try
                {
                    return (EnumDeviceParamType)ParamID;
                }
                catch (Exception)
                {
                    return EnumDeviceParamType.保留;
                }
            }
        }

        [JsonPropertyName("参数值")]
        public string Data { get; set; }

        public DpString(uint id)
        {
            ParamID = id;
        }

        public DpString(string data)
        { 
            Data = data;
        }

        public DpString(uint id, string data)
        {
            ParamID = id;
            Data = data;
            Length = (byte)(ParseHelper.GbkBytes(data).Length + 5);
        }

        public void ReadEntity(ref ReadOnlySpan<byte> source,ref int startIndex)
        {
            ParamID = ParseHelper.Uint32(ref source, startIndex);
            startIndex += 4;
            Length = source[startIndex];
            startIndex++;
            Data = ParseHelper.Gb2312String(ref source,startIndex,Length);
            startIndex += Length;
        }

        public List<byte> WriteToNetBytes()
        {
            return new List<byte>(ParseHelper.GbkBytes(Data));
        }
    }
}